package com.bridge.client.bridgedynamcclient.controller;

import com.bridge.client.bridgedynamcclient.utils.FXMLUtil;
import com.bridge.client.bridgedynamcclient.utils.StringUtil;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

import java.net.URL;
import java.util.Objects;
import java.util.ResourceBundle;

public class BridgeWidthCalculateController extends BaseController implements Initializable {

    @FXML
    private TextField beMotorWayNumber;
    @FXML
    private TextField beNonMotorWayNumber;
    @FXML
    private TextField beSideWalkNumber;
    @FXML
    private Button btnOk;

    private BridgeAddMainController bridgeAddMainController;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        initListener();
    }

    private void initListener() {
        btnOk.setOnAction(a -> {
            String motorWayNumber = beMotorWayNumber.getText();
            if (StringUtil.isEmpty(motorWayNumber)) {
                FXMLUtil.showAlertInfo("请输入机动车道总数");
                return;
            }
            String nonMotorWayNumber = beNonMotorWayNumber.getText();
            if (StringUtil.isEmpty(nonMotorWayNumber)) {
                FXMLUtil.showAlertInfo("请输入非机动车道数");
                return;
            }
            String sideWalkNumber = beSideWalkNumber.getText();
            if (StringUtil.isEmpty(sideWalkNumber)) {
                FXMLUtil.showAlertInfo("请输入行道数");
                return;
            }
            BridgeAddMainController.WidthParam paramNew = new BridgeAddMainController.WidthParam();
            paramNew.setMotorWayNumber(motorWayNumber);
            paramNew.setNonMotorWayNumber(nonMotorWayNumber);
            paramNew.setSideWayNumber(sideWalkNumber);
            //把结果回调出去
            double result = 2 + 3.5 * (Double.parseDouble(motorWayNumber) + Double.parseDouble(nonMotorWayNumber) + Double.parseDouble(sideWalkNumber));
            bridgeAddMainController.onBridgeWidthCalculate(String.valueOf(result), paramNew);


            //关闭当前页面
            FXMLUtil.closeState(btnOk.getScene());
        });
    }

    public void setConfig(BridgeAddMainController.WidthParam paramNew, BridgeAddMainController bridgeAddMainController) {
        this.bridgeAddMainController = bridgeAddMainController;
        if (Objects.nonNull(paramNew)) {
            beMotorWayNumber.setText(paramNew.getMotorWayNumber());
            beNonMotorWayNumber.setText(paramNew.getNonMotorWayNumber());
            beSideWalkNumber.setText(paramNew.getSideWayNumber());
        }
    }
}
